package com.rxt.thermopro.viewmodel

import androidx.lifecycle.MutableLiveData
import com.app.common.ex.logD
import com.app.common.viewmodel.BaseViewModel
import com.rxt.ble.BleManager
import com.rxt.ble.callback.BleWriteCallback
import com.rxt.ble.exception.BleException
import com.rxt.ble.utils.HexUtil
import com.rxt.thermopro.bean.DeviceBean
import com.rxt.thermopro.viewmodel.BleMessageViewModel.Companion.BLUETOOTH_SERVICE_UUID_359
import com.rxt.thermopro.viewmodel.BleMessageViewModel.Companion.BLUETOOTH_WRITE_UUID_359

class DataChartsViewModel : BaseViewModel() {

    val notifyDeviceMsgData: MutableLiveData<DeviceBean> = MutableLiveData()

    /**
     * 连接蓝牙接收通知  (1=一天  2=一周 3=一年)
     */
    fun getTempHistory(timeType: Int, address: String) {
        var endMark=""
        val startMark = when (timeType) {
            1 -> {
                endMark="7A"
                "A7"
            }
            2 -> {
                endMark="6A"
                "A6"
            }
            3 -> {
                endMark="8A"
                "A8"
            }
            else -> {
                endMark="7A"
                "A7"
            }
        }
        val msg = "${startMark}0C00${endMark}"
        println("getTempHistory msg====$msg")
        val deviceList = BleManager.getInstance().allConnectedDevice.filter { it.mac == address }
        if (deviceList.isNotEmpty()) {
            BleManager.getInstance().write(
                deviceList[0],
                BLUETOOTH_SERVICE_UUID_359,
                BLUETOOTH_WRITE_UUID_359,
                HexUtil.hexStringToBytes(msg),
                object : BleWriteCallback() {
                    override fun onWriteSuccess(
                        current: Int,
                        total: Int,
                        justWrite: ByteArray
                    ) {
                        // 发送数据到设备成功
                        logD(" 蓝牙发送数据成功 getTempHistory  $msg")

                    }

                    override fun onWriteFailure(exception: BleException) {
                        logD(" 蓝牙发送数据失败  ${exception.toString()}")
                        // 发送数据到设备失败

                    }
                })
        }
    }

}